<?php

  class PicAction extends CommonAction{
     
	 public function index(){
	 
	 
		
		   
		 //  获取图片二进制编码字符串

           $re = $this->pic64($_POST['name']);
           //验证图片级别
           $picinfo = $this->picyanzheng($re);
           if($picinfo>=65){
           	//不符合要求则删除并返回
           	unlink("./images/".$_POST['name']);

           }else{
           	$pic     = "./images/".$_POST['name'];
           	$picblur = "./images/b-".$_POST['name'];
			
			include ("./mohu.php");
			
			
           $test=new test();

            $mohu=$test->gaussian_blur('./images/'.$_POST['name'],'','b-'.$_POST['name'],3);
			
			if($mohu){
			
			    import('ORG.Util.Image');
				$Image = new Image();
				$Image->water($picblur,"./images/logo.png");
			 
			 
			  $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$_POST['token']."&type=image";
				$file = realpath("./images/b-".$_POST['name']); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);

				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$_POST['token'];
                $data = '{
                    "touser": "'.$_POST['openid'].'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
			
			}
			
           //	模糊图片
          // 	$bool = $this->picblur($pic,$picblur,60);
           	if($bool==true){
			
			  //将图片写入数据库
				$data['time']    = time();
				$data['qingxi']  = $_POST['name'];
				$data['mohu']    = "blur-".$_POST['name'];
				$re = M('orderinfo')->add($data);
	          //  加水印
	            import('ORG.Util.Image');
				$Image = new Image();
				$Image->water($picblur,"./images/logo.png");
				
				if($re){
			   //上传图片到临时素材获取media_id
               $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$_POST['token']."&type=image";
				$file = realpath("./images/blur-".$_POST['name']); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);

				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$_POST['token'];
                $data = '{
                    "touser": "'.$_POST['openid'].'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
	            
				}
           	}
 	      
	       }
		
	 
	 }
	  public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        //return $output;
    }


  
  }